package com.zdsoft.finance.organization.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.essential.client.service.CED;
import com.zdsoft.essential.dto.basedata.SimpleCodeDto;
import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.organization.entity.LoanOrganization;
import com.zdsoft.finance.organization.repository.LoanOrganizationRepository;
import com.zdsoft.finance.organization.service.LoanOrganizationService;
import com.zdsoft.framework.core.common.util.ObjectHelper;

@Service
public class LoanOrganizationServiceImpl extends BaseServiceImpl<LoanOrganization, CustomRepository<LoanOrganization, String>>
implements LoanOrganizationService {

	@Autowired
	private LoanOrganizationRepository loanOrganizationRepository;
	
	/**
	 * 查询simpleCode
	 * @param CED 基础数据系统
	 * @return 合作机构
	 * @throws BusinessException 异常
	 */
	private List<SimpleCodeDto> getSimpleCode(CED CED) throws BusinessException{
		if(ObjectHelper.isEmpty(CED)){
			throw new BusinessException("100000001","CED为空");
		}
		List<String> categoryCds = new ArrayList<>();
		categoryCds.add("YWDM0001");
		List<SimpleCodeDto> list = null;
		try {
			list = CED.querySimpleCodeByCategoryCds(categoryCds);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("simple查询出错",e.getMessage());
		}
		return list;
	}
	
	@Override
	public String findJsonAll(CED CED) throws BusinessException {
		List<SimpleCodeDto> list = this.getSimpleCode(CED);
		if(ObjectHelper.isEmpty(list) || list.size() == 0){
			throw new BusinessException("100000002","simpleCode未找到值");
		}
		List<Map<String,Object>> LM = new ArrayList<>();
		for (SimpleCodeDto dto : list) {
			Map<String,Object> map = this.getMap(dto.getId(), dto.getName(), dto.getFullCode(), "");
			LM.add(map);
			List<LoanOrganization> orgList = loanOrganizationRepository.findByOrgType(dto.getFullCode());
			if(ObjectHelper.isEmpty(orgList) || orgList.size() == 0){
				continue;
			}
			for (LoanOrganization org : orgList) {
				if(ObjectHelper.isEmpty(org.getParent())){
					Map<String,Object> orgMap = this.getMap(org.getId(), org.getOrgName(), org.getOrgType(), dto.getId());
					LM.add(orgMap);
				}else{
					Map<String,Object> orgMap = this.getMap(org.getId(), org.getOrgName(), org.getOrgType(), org.getParent().getId());
					LM.add(orgMap);
				}
			}
		}
		return ObjectHelper.objectToJson(LM);
	}
	
	/**
	 * map封装
	 * @param id
	 * @param name
	 * @param orgType
	 * @param parendId
	 * @return
	 */
	private Map<String,Object> getMap(String id,String name,String orgType,String parendId){
		if(ObjectHelper.isEmpty(id) || ObjectHelper.isEmpty(name) || ObjectHelper.isEmpty(orgType)){
			return null;
		}
		Map<String,Object> map = new HashMap<>();
		map.put("id", id);
		map.put("name", name);
		map.put("orgType", orgType);
		map.put("parendId", parendId == null ? "" : parendId);
		return map;
	}

	@Override
	@Transactional(rollbackFor = BusinessException.class)
	public LoanOrganization saveOrUpdate(LoanOrganization po) throws BusinessException {
		LoanOrganization bean = null;
		if(ObjectHelper.isNotEmpty(po.getParent_id())){
			//保存子
			LoanOrganization parent = loanOrganizationRepository.findOne(po.getParent_id());
			po.setId(null);
			po.setParent(parent);
			bean = loanOrganizationRepository.saveEntity(po);
			parent.getChildren().add(bean);
			loanOrganizationRepository.updateEntity(parent);
		}else{
			//普通保存
			if(ObjectHelper.isNotEmpty(po.getId())){
				bean = loanOrganizationRepository.findOne(po.getId());
			}
			if(ObjectHelper.isNotEmpty(bean)){
				//修改
				bean.setOrgType(po.getOrgType());
			    bean.setOrgName(po.getOrgName());
			    bean.setOrgFullName(po.getOrgFullName());
			    bean.setOrgCode(po.getOrgCode());
			    bean.setContactMan(po.getContactMan());
			    bean.setTelephone(po.getTelephone());
			    bean.setRemark(po.getRemark());
			    bean.setAutoRenew(po.getAutoRenew());
			    bean.setEnable(po.getEnable());
			    bean.setSearchCode(po.getSearchCode());
			    bean.setCooperationAttachment(po.getCooperationAttachment());
			    bean.setCreditBeginDate(po.getCreditBeginDate());
			    bean.setCreditEndDate(po.getCreditEndDate());
			    bean.setCreditAmount(po.getCreditAmount());
			    bean.setCreditBalance(po.getCreditBalance());
			    bean = loanOrganizationRepository.updateEntity(bean);
			}else{
				//添加
				bean = loanOrganizationRepository.saveEntity(po);
			}
		}
		return bean;
	}

	@Override
	@Transactional(rollbackFor = BusinessException.class)
	public void updateEnable(String id, Boolean enable) throws BusinessException {
		if(ObjectHelper.isEmpty(id)){
			throw new BusinessException("100000001","更改状态的id未空");
		}
		LoanOrganization bean = loanOrganizationRepository.getOne(id);
		if(ObjectHelper.isEmpty(bean)){
			throw new BusinessException("100000001","更改状态的id未找到数据");
		}
		bean.setEnable(enable);
		loanOrganizationRepository.updateEntity(bean);
	}

}
